﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Softyum.ForexAgent.Site;
using System.IO;

namespace Softyum.ForexAgent.Forms
{
    public partial class SiteExplorerForm : Form
    {
        private static readonly string NEW_CATEGORY_NAME_PREFIX = "新类别";
        private static readonly int NODE_LEVEL_ROOT = 0;
        private static readonly int NODE_LEVEL_CATEGORY = 1;
        private static readonly int NODE_LEVEL_PAGE = 2;

        private NewsSourceManager websiteManager;

        private TreeNode Root { get { return treeView.Nodes[0]; } }

        public SiteExplorerForm()
        {
            //
            websiteManager = Program.NewsSourceManager;
            //
            InitializeComponent();
        }

        private void InitializeUI()
        {
            TreeNode siteNode = Root;
            foreach (WebSiteCategory wc in websiteManager.Categories)
            {
                //
                TreeNode categoryNode = siteNode.Nodes.Add(wc.Name, wc.Name);
                categoryNode.ContextMenuStrip = contextMenuStripCategory;
                categoryNode.Tag = wc;
                //
                foreach (WebPage page in wc.Pages)
                {
                    TreeNode pageNode = categoryNode.Nodes.Add(page.Name, page.Title);
                    pageNode.Tag = page;
                }
            }

            //
            siteNode.ExpandAll();
        }

        private string CreateNewCategoryName()
        {
            TreeNode siteNode = Root;
            for (int i = 1; i < int.MaxValue; i++)
            {
                string name = string.Format("{0}({1})",
                    NEW_CATEGORY_NAME_PREFIX, i);
                if ((siteNode.Nodes[name] == null) && 
                    !websiteManager.IsCategoryExist(name)) //
                {
                    return name;
                }
            }

            //
            return null;
        }

        #region Form Events
        private void SiteExplorerForm_Load(object sender, EventArgs e)
        {
            InitializeUI();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            Close();
        }
        #endregion

        #region Menu Events
        private void addNewCategoryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string categoryName = CreateNewCategoryName();
            if (!string.IsNullOrEmpty(categoryName))
            {
                int idx = websiteManager.AddCategory(categoryName);
                WebSiteCategory category = websiteManager.Categories[idx];
                TreeNode node = Root.Nodes.Add(categoryName);
                node.ContextMenuStrip = contextMenuStripCategory;
                node.Tag = category;
                node.BeginEdit();
            }
        }

        private void deleteCategoryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNode node = treeView.SelectedNode;
            WebSiteCategory wsc = node.Tag as WebSiteCategory;
            bool bDel = true;
            if (wsc.Pages.Count > 0)
            {
                DialogResult dr = MessageBox.Show(
                    string.Format("『{0}』下还有{1}个新闻源,是否删除?", wsc.Name, wsc.Pages.Count), 
                    Application.ProductName, 
                    MessageBoxButtons.YesNo, MessageBoxIcon.Question, 
                    MessageBoxDefaultButton.Button1);
                if (dr == System.Windows.Forms.DialogResult.No)
                {
                    bDel = false;
                }
            }
            if (bDel)
            {
                websiteManager.Delete(wsc);
                Root.Nodes.Remove(node);
            }
        }

        /// <summary>
        /// 添加新闻源
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addSourceToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
        #endregion

        #region TreeView Events
        private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            //WebSiteCategory wsc = e.
            if (e.Node.Level == NODE_LEVEL_ROOT)
            {
            }
            else if (e.Node.Level == NODE_LEVEL_CATEGORY) // category
            {

            }
            else if (e.Node.Level == NODE_LEVEL_PAGE) // page
            {
                //WebSiteCategory wsc = e.Node.Parent.Tag as WebSiteCategory;
                WebPage wp = e.Node.Tag as WebPage;
                //var v = (from p in wsc.Pages
                //        where p.Name.Equals(wp.Name)
                //        select p).First();
                propertyGrid.SelectedObject = wp;
            }
            else
            {
                throw new Exception(String.Format(
                    "Level {0} not supported", e.Node.Level));
            }
        }
        private void treeView_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
        {
            if (e.Node.Level == NODE_LEVEL_ROOT)
            {
                e.CancelEdit = true;
            }
            else if (e.Node.Level == NODE_LEVEL_CATEGORY) // category
            {
                if (!string.IsNullOrEmpty(e.Label))
                {
                    if (!websiteManager.IsCategoryExist(e.Label))
                    {
                        WebSiteCategory wsc = e.Node.Tag as WebSiteCategory;
                        wsc.Rename(e.Label);
                    }
                    else
                    {
                        e.CancelEdit = true;
                        MessageBox.Show(string.Format("{0} 已经存在", e.Label),
                            Application.ProductName, 
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    e.CancelEdit = true;
                }
            }
            else if (e.Node.Level == NODE_LEVEL_PAGE) // page
            {
            }
            else
            {
                throw new Exception(String.Format(
                    "Level {0} not supported", e.Node.Level));
            }
        }
        #endregion

    }
}
